﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Localization;
using SinBiota.Model.Business.PersistenceServices.Interfaces;
using SinBiota.Model.Entities;
using Sinbiota.Web.ActionFilters;
using Sinbiota.Web.Models;

namespace Sinbiota.Web.Controllers
{
    public class MunicipalityController : BaseController
    {
        private readonly IMunicipalityPersistenceServices _municipalityPersistenceServices;
        private readonly IDrainageBasinPersistenceServices _drainageBasinPersistenceServices;

        public MunicipalityController(IMunicipalityPersistenceServices municipalityPersistenceServices, IUserPersistenceServices userPersistenceServices, IDrainageBasinPersistenceServices drainageBasinPersistenceServices)
        {
            _municipalityPersistenceServices = municipalityPersistenceServices;
            _drainageBasinPersistenceServices = drainageBasinPersistenceServices;
        }

        //
        // GET: /Municipality/
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Index()
        {
            return View(_municipalityPersistenceServices.FindByState(State.AC));
        }

        // POST: /Municipality/ State selected
        [HttpPost]
        [AdminPermission]
        public ActionResult Index(FormCollection collection, State? state)
        {
             return View(_municipalityPersistenceServices.FindByNameLike(collection[SearchBoxName], state));
        }


        //
        // GET: /Municipality/Details/5
        [AdminPermission]
        public ActionResult Details(int id)
        {
            var municipality = _municipalityPersistenceServices.FindById(id);
            return municipality == null ? View("NotFound") : View(municipality);
        }

        //
        // GET: /Municipality/Create
        [ImportMessagesFromTempData]
        [AdminPermission]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Municipality/Create
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Create(FormCollection collection, Municipality municipality, int drainageBasinId)
        {
            if (ModelState.IsValid)
            {
                municipality.DrainageBasin = _drainageBasinPersistenceServices.FindById(drainageBasinId);
                _municipalityPersistenceServices.Save(municipality);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View();
        }

        //
        // GET: /Municipality/Edit/5
        [AdminPermission]
        public ActionResult Edit(int id)
        {
            var municipality = _municipalityPersistenceServices.FindById(id);
            return municipality != null ? View(municipality) : View("NotFound");
        }

        //
        // POST: /Municipality/Edit/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Edit(int id, FormCollection collection, Municipality municipality, int drainageBasinId)
        {
            var editMunicipality = _municipalityPersistenceServices.FindById(id);
            TryUpdateModel(editMunicipality);
            if (ModelState.IsValid)
            {
                editMunicipality.DrainageBasin = _drainageBasinPersistenceServices.FindById(drainageBasinId);
                _municipalityPersistenceServices.Update(editMunicipality);
                SetInformationMessage(Messages.OperationCompletedSuccessfully);
                return RedirectToAction("Index");
            }
            return View(municipality);
        }

        //
        // GET: /Municipality/Delete/5
        [AdminPermission]
        public ActionResult Delete(int id)
        {
            var municipality = _municipalityPersistenceServices.FindById(id);
            return municipality != null ? View(municipality) : View("NotFound");
        }

        //
        // POST: /Municipality/Delete/5
        [HttpPost, ExportMessagesToTempData]
        [AdminPermission]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var municipality = _municipalityPersistenceServices.FindById(id);
            if (municipality == null) return View("NotFound");
            _municipalityPersistenceServices.Delete(municipality);
            SetInformationMessage(Messages.OperationCompletedSuccessfully);
            return RedirectToAction("Index");
        }

        // AJAX: 
        //Get all Municipalities (only in SP, for the occurrence select box);
        public JsonResult GetAll()
        {
            var list = _municipalityPersistenceServices.FindByState(State.SP).Select(x => new { x.Id, x.Name}).OrderBy(i => i.Name);
            return Json(list);
        }
        public JsonResult GetFromState(State state)
        {
            var list = _municipalityPersistenceServices.FindByState(state).Select(x => new { x.Id, x.Name }).OrderBy(i => i.Name);
            return Json(list);
        }
    }
}
